<?php include '/../header.php';?>
    
<link rel="stylesheet" type="text/css" href="<?php echo css_url().'body_general.css'?>">
<link rel="stylesheet" type="text/css" href="<?php echo css_url().'table/list_show.css'?>">
<link rel="stylesheet" type="text/css" href="<?php echo css_url().'JPag.css'?>" media="screen"/>
<link rel="stylesheet" type="text/css" href="<?php echo css_url().'960grid/960.css'?>">

<!--- Dialog Addon --->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css">
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>

<section id='body'>
    <div class="left-section">
        
    </div>
    <link rel="stylesheet" type="text/css" href="<?php echo css_url().'general_form.css'?>">
    <div class="main-section">
        <div class="title tab">
            <b>Post Management: </b>
            <div id="enable-post" class="margin-left add-btn" style='position:relative; left: 11%'>Enable</div>
        </div>
        <div class="content-section margin-bottom" >
            <?php 
                if(count($UserPosts) ==0){
                    echo "<div class='annoucement tab' >You have no disable post. You can disable a post by click the disable button</div>";
                }else{
                    $CI =& get_instance();
                    $CI->load->library('table');
                    $data = array();
                    $data[] = array('','Subject','Field', 'Date');

                    $checkbox_number = 0;
                    foreach($UserPosts as $Post){
                        $data[] = array("<input ids='$checkbox_number' name='checkbox' type='checkbox' postid='".$Post['PostID']."'>"
                                ,$Post['Subject']
                                ,$Post['CategoryName']
                                ,$Post['PostDate']);
                        $checkbox_number++;
                    }
                    $tmpl = array(
                            'table_open'    =>  '<table class="list_show">',
                    );
                    $CI->table->set_template($tmpl);
                    echo $CI->table->generate($data)    ;
                }
            ?>
        </div>
        
        <?php if(!empty($Posts)):?>
        <div id="pagination-container" style="float: right; width:30%;">
                    <div id="pagination"> </div>
        </div>
        <?php endif; ?>
    </div>
</section>

<script src="<?php echo js_url().'jquery.paginate.js'?>" type="text/javascript"></script>
<script>  
    var TotalNum = <?php echo $TotalNum?>;
    $(document).ready(function(){
        //Handling table
        $("tr input").each(function(){
            $(this).parent().attr('class','checkbox');
        });
        
        $("[name='checkbox']").on('click',function(e){
            e.stopPropagation();
        });
        $("tbody tr").each(function(){
            postid = $(this).children('td.checkbox').children('input').attr("postid");
            $(this).attr('data-href','<?php echo site_url()?>/user_group2/posts/'+postid);
        });
        
         $('table tbody tr td[class!="checkbox"]').click(function(){
            window.location = $(this).parent().data('href');
            return false;
        });
        //End of handling Table
        
        //Handling buttons
        $("#add-post").on("click",function(){
            $.get("<?php echo site_url().'/user_group2/posts/post_create_page' ?>", function(data){
                    $(".main-section").empty();
                    $(".main-section").html(data);
            });
        });
        
        $("#enable-post").on("click",function(){
            value = new Array();
            $("[name='checkbox']:checked").each(function(){
                value.push($(this).attr('postid'));
            });
            
            ConfirmDialog("Are you sure want to enable these posts",enable_posts,value);
            
            
        });
        
        var enable_posts = function(ListID){
            $.ajax({
                url : '<?php echo site_url();?>/user_group2/posts/enable_posts',
                type: 'post',
                data: {ListID : ListID},
                async: false,
                success: function(data){
                    if(data == 1 || data == '1')
                    {
                        alert('Successfully Enable the posts');
                        window.location.href = '<?php echo curPageURL(); ?>'
                    }
                    else{
                        alert("Failed to Enable these posts. Please try again");
                    }
                } 
            });
        }
        
        //Confirm Dialog
        function ConfirmDialog(message, okfunction,value){
            $('<div></div>').appendTo('body')
                           .html('<div><h6>'+message+'?</h6></div>')
                           .dialog({
                               modal: true, title: 'Delete message', zIndex: 10000, autoOpen: true,
                               width: 'auto', resizable: false,
                               buttons: {
                                   Yes: function () {
                                       // $(obj).removeAttr('onclick');                                
                                       // $(obj).parents('.Parent').remove();

                                       $(this).dialog("close");
                                       okfunction(value);
                                   },
                                   No: function () {
                                       $(this).dialog("close");
                                   }
                               },
                               close: function (event, ui) {
                                   $(this).remove();
                               }
                           });
           };
    });
    
    
    
    $(function() {
            $("#pagination").paginate({
                    count 		: Math.ceil(TotalNum/10),
                    start 		: <?php echo $Current_Page; ?>,
                    display     : 7,
                    border					: true,
                    border_color			: '#fff',
                    text_color  			: '#FB6280',
                    background_color    	: '#FFF',	
                    border_hover_color		: '#FB6280',
                    text_hover_color  		: '#FFF',
                    background_hover_color	: '#FB6280', 
                    images					: false,
                    mouse					: 'mousedown',
                    link                        : '<?php echo curPageURL().'?Page=' ?>',
                    onChange     			: function(page){

                    }
            });
    });
    
</script>

<?php include '/../../footer.php';?>